package com.ark.yygh.hosp.controller.api;

import com.ark.yygh.common.exception.YyghException;
import com.ark.yygh.common.helper.HttpRequestHelper;
import com.ark.yygh.common.result.Result;
import com.ark.yygh.common.result.ResultCodeEnum;
import com.ark.yygh.common.utils.MD5;
import com.ark.yygh.hosp.service.DepartmentService;
import com.ark.yygh.hosp.service.HospitalService;
import com.ark.yygh.hosp.service.HospitalSetService;
import com.ark.yygh.hosp.service.ScheduleService;
import com.ark.yygh.model.hosp.Department;
import com.ark.yygh.model.hosp.Hospital;
import com.ark.yygh.model.hosp.Schedule;
import com.ark.yygh.vo.hosp.DepartmentQueryVo;
import com.ark.yygh.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Api(tags = "医院管理API接口")
@RestController
@RequestMapping("/api/hosp")
public class ApiController {

    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private HospitalSetService hospitalSetService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private ScheduleService scheduleService;

    @ApiOperation("上传医院")
    @PostMapping("/saveHospital")
    public Result saveHospital(HttpServletRequest request){
        //获取传递过来的医院信息
        Map<String, Object> switchMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //获取医院系统传递过来的签名
        String sign = (String) switchMap.get("sign");
        //获取医院的编码
        String hoscode = (String) switchMap.get("hoscode");
        //查询mysql数据库的签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        //加密数据库的签名
        String encrypt = MD5.encrypt(signKey);
        //医院系统签名和数据库加密的签名对比
        if (!sign.equals(encrypt)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //在传输过程中，图片路径里的+转为了 " "，最后要转回来，否则图片无法正常显示
        String logoData = (String) switchMap.get("logoData");
        if (!StringUtils.isEmpty(logoData)){
            String replace = logoData.replaceAll(" ", "+");
            switchMap.put("logoData",replace);
        }
        //调用方法上传
        hospitalService.save(switchMap);
        return Result.ok();
    }

    @ApiOperation("获取医院信息")
    @PostMapping("/hospital/show")
    public Result getHospital(HttpServletRequest request){
        Map<String, Object> switchMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //获取医院系统传递过来的签名
        String sign = (String) switchMap.get("sign");
        //获取医院的编码
        String hoscode = (String) switchMap.get("hoscode");
        //查询mysql数据库的签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        //加密数据库的签名
        String encrypt = MD5.encrypt(signKey);
        //医院系统签名和数据库加密的签名对比
        if (!sign.equals(encrypt)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //调用方法
        Hospital hospital = hospitalService.getHospitalByHoscode(hoscode);
        return Result.ok(hospital);
    }

    @ApiOperation("上传科室")
    @PostMapping("/saveDepartment")
    public Result saveDepartment(HttpServletRequest request){
        Map<String, Object> switchMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //获取医院系统传递过来的签名
        String sign = (String) switchMap.get("sign");
        //获取医院的编码
        String hoscode = (String) switchMap.get("hoscode");
        //查询mysql数据库的签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        //加密数据库的签名
        String encrypt = MD5.encrypt(signKey);
        //医院系统签名和数据库加密的签名对比
        if (!sign.equals(encrypt)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //调用service方法
        departmentService.save(switchMap);
        return Result.ok();
    }


    @ApiOperation("查询科室列表")
    @PostMapping("/department/list")
    public Result findDepartment(HttpServletRequest request){
        Map<String, Object> switchMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //分页  page:当前页   limit：显示条数
        int page = StringUtils.isEmpty(switchMap.get("page")) ? 1 : Integer.parseInt((String) switchMap.get("page"));
        int limit = StringUtils.isEmpty(switchMap.get("limit")) ? 5 : Integer.parseInt((String) switchMap.get("limit"));

        //获取医院系统传递过来的签名
        String sign = (String) switchMap.get("sign");
        //获取医院的编码
        String hoscode = (String) switchMap.get("hoscode");
        //查询mysql数据库的签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        //加密数据库的签名
        String encrypt = MD5.encrypt(signKey);
        //医院系统签名和数据库加密的签名对比
        if (!sign.equals(encrypt)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        //调用方法
        Page<Department> departmentPage = departmentService
                .findPageDepartment(page,limit,departmentQueryVo);

        return Result.ok(departmentPage);
    }

    @ApiOperation(value = "删除科室")
    @PostMapping("/department/remove")
    public Result removeDepartment(HttpServletRequest request) {
        Map<String, Object> switchMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //获取医院系统传递过来的签名
        String sign = (String) switchMap.get("sign");
        //获取医院的编码
        String hoscode = (String) switchMap.get("hoscode");
        //查询mysql数据库的签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        //加密数据库的签名
        String encrypt = MD5.encrypt(signKey);
        //医院系统签名和数据库加密的签名对比
        if (!sign.equals(encrypt)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //获取科室编号
        String depcode = (String) switchMap.get("depcode");
        //调用方法
        departmentService.remove(hoscode,depcode);
        return Result.ok();
    }

    @ApiOperation(value = "上传排班")
    @PostMapping("/saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {
        Map<String, Object> switchMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //获取医院系统传递过来的签名
        String sign = (String) switchMap.get("sign");
        //获取医院的编码
        String hoscode = (String) switchMap.get("hoscode");
        //查询mysql数据库的签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        //加密数据库的签名
        String encrypt = MD5.encrypt(signKey);
        //医院系统签名和数据库加密的签名对比
        if (!sign.equals(encrypt)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        scheduleService.save(switchMap);
        return Result.ok();
    }

    @ApiOperation(value = "查询排班列表")
    @PostMapping("schedule/list")
    public Result schedule(HttpServletRequest request){
        Map<String, Object> switchMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //获取医院系统传递过来的签名
        String sign = (String) switchMap.get("sign");
        //获取医院的编码
        String hoscode = (String) switchMap.get("hoscode");
        //查询mysql数据库的签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        //加密数据库的签名
        String encrypt = MD5.encrypt(signKey);
        //医院系统签名和数据库加密的签名对比
        if (!sign.equals(encrypt)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        int page = StringUtils.isEmpty(switchMap.get("page")) ? 1 : Integer.parseInt((String) switchMap.get("page"));
        int limit = StringUtils.isEmpty(switchMap.get("limit"))? 1 : Integer.parseInt((String) switchMap.get("limit"));

        //获取科室编号
        String depcode = (String) switchMap.get("depcode");

        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(depcode);
        Page<Schedule> pageModel = scheduleService.selectPage(page,limit,scheduleQueryVo);
        return Result.ok(pageModel);
    }

    @ApiOperation(value = "删除科室")
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request){
        Map<String, Object> switchMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //获取医院系统传递过来的签名
        String sign = (String) switchMap.get("sign");
        //获取医院的编码
        String hoscode = (String) switchMap.get("hoscode");
        //查询mysql数据库的签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        //加密数据库的签名
        String encrypt = MD5.encrypt(signKey);
        //医院系统签名和数据库加密的签名对比
        if (!sign.equals(encrypt)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        //获取排班编号
        String hosScheduleId = (String) switchMap.get("hosScheduleId");
        //调用方法
        scheduleService.remove(hoscode,hosScheduleId);
        return Result.ok();
    }
}
